package com.zj.base.base_fg;

import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ZhaoJie on 2018/7/7.
 */

@SuppressWarnings("unused")
public abstract class BaseFragmentManager extends FragmentHelper {

    private List<View> indicatorViews;

    public BaseFragmentManager(@NonNull FragmentActivity activity, ViewGroup parent, @IdRes int containerId, int curItem, Fragment... fragments) {
        super(activity.getSupportFragmentManager(), containerId, Arrays.asList(fragments));
        if (indicatorViews == null) indicatorViews = new ArrayList<>();
        for (int i = 0; i < parent.getChildCount(); i++) {
            View v = parent.getChildAt(i);
            onViewAttach(v);
            indicatorViews.add(v);

        }
        int fragmentSize = fragments.length;
        if (indicatorViews.size() != fragmentSize) {
            throw new IllegalStateException(getClass().getSimpleName() + " error case : you can`t use a different size in fragments and childViews");
        }
        show(curItem);
    }

    //override this method to build your childView
    public abstract void onViewAttach(View v);

    private void show(int curItem) {
        if (curItem >= indicatorViews.size()) {
            throw new IllegalStateException(getClass().getSimpleName() + " error case : current display Item position should not greater than fragments size");
        }
        showFragment(curItem);
        initViews();
    }

    public void selectedFragment(int position) {
        if (indicatorViews != null) {
            View v = indicatorViews.get(position);
            if (v != null)
                v.callOnClick();
        }
    }

    private void initViews() {
        for (int i = 0; i < indicatorViews.size(); i++) {
            View v = indicatorViews.get(i);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showFragment(indicatorViews.indexOf(v));
                }
            });
        }
    }

    @Override
    public void beginTransaction(FragmentTransaction transaction, int oldItem, int curItem, int maxIndex) {
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    }

    @Override
    public final void syncSelectState(int selectImage) {
        if (indicatorViews != null && indicatorViews.size() > selectImage) {
            for (int i = 0; i < indicatorViews.size(); i++) {
                View v = indicatorViews.get(i);
                onSelectState(v, i == selectImage);
            }
        }
    }

    private void onSelectState(View v, boolean isSelected) {
        if (v.isSelected() != isSelected)
            v.setSelected(isSelected);
    }

    @Override
    public boolean whenShowSameFragment(int showItem) {
        return false;
    }

    @Override
    public boolean whenShowNotSameFragment(int showItem) {
        return true;
    }
}